package com.menu.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.menu.Mapper.CollectionMapper;
import com.menu.Mapper.MenuFatherMapper;
import com.menu.Mapper.MenuMapper;
import com.menu.Mapper.UserMapper;
import com.menu.Pojo.Collection;
import com.menu.Pojo.Menu;
import com.menu.Pojo.MenuFather;
import com.menu.Pojo.User;
import com.menu.Service.CollectionService;
import com.menu.Service.NoticeService;
import com.menu.Utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Resource
    CollectionMapper collectionMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    MenuMapper menuMapper;
    @Resource
    MenuFatherMapper menuFatherMapper;
    @Resource
    NoticeService noticeService;

    @Override
    public ResponseResult collection(Integer userId, Integer menuId,Integer menuType) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }
        if (menuType == 0) {
            Menu menu= menuMapper.findByMenuId(menuId);
            if (menu == null) {
                return ResponseResult.error("食谱不存在");
            }
         ResponseResult responseResult=noticeService.addCollectionMenuNotice(menuId,userId,menu.getUserId());
            if(responseResult.getCode()!=200){
                return responseResult;
            }
        }
        if (menuType == 1) {
            MenuFather menu= menuFatherMapper.findByMenuFatherId(menuId);
            if (menu == null) {
                return ResponseResult.error("菜单不存在");
            }
            ResponseResult responseResult=noticeService.addCollectionMenuFatherNotice(menuId,userId,menu.getUserId());
            if(responseResult.getCode()!=200){
                return responseResult;
            }
        }
        Collection collection= collectionMapper.findByUserIdAndMenuId(userId, menuId,menuType);
        if(collection==null){
            collectionMapper.addCollection(userId,menuId,menuType);
            Integer i=collectionMapper.countCollection(menuId,menuType);
            return ResponseResult.success("收藏成功",i);
        }else{
            collectionMapper.deleteCollection(userId, menuId,menuType);
            Integer i=collectionMapper.countCollection(menuId,menuType);
            return ResponseResult.success("取消收藏成功",i);
        }


    }

    @Override
    public ResponseResult judgeOfCollection(Integer userId, Integer menuId,Integer menuType) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }
        if(menuType==0){
            Menu menu= menuMapper.findByMenuId(menuId);
            if (menu == null) {
                return ResponseResult.error("食谱不存在");
            }
            Collection collection= collectionMapper.findByUserIdAndMenuId(userId, menuId,menuType);
            if (collection == null) {
                return ResponseResult.success("未收藏",0);
            }else{
                return ResponseResult.success("已经收藏",1);
            }
        }else{
            MenuFather menu= menuFatherMapper.findByMenuFatherId(menuId);
            if (menu == null) {
                return ResponseResult.error("菜单不存在");
            }
            Collection collection= collectionMapper.findByUserIdAndMenuId(userId, menuId,menuType);
            if (collection == null) {
                return ResponseResult.success("未收藏",0);
            }else{
                return ResponseResult.success("已经收藏",1);
            }
        }

    }

    @Override
    public ResponseResult findCollectionByUserId(Integer userId,Integer menuType,Integer pageNow,Integer pageSize) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }

        Integer[]i= collectionMapper.findCollectionIdByUserId(userId,menuType);
        PageHelper.startPage(pageNow,pageSize);

        if(menuType==0){
            List<Menu> menuList=new ArrayList<>();
            for(int x:i){
                Menu menu=  menuMapper.findByMenuId(x);
                if(menu!=null){
                    if(menu.getState()==1){
                        menuList.add(menu);
                    }
                }
            }
            PageInfo<Menu> pageInfo=new PageInfo<>(menuList);
            return ResponseResult.success("用户食谱收藏遍历如下", pageInfo);
        }
        if (menuType == 1) {
            List<MenuFather> menuList=new ArrayList<>();
            for(int x:i){
                MenuFather menu=  menuFatherMapper.findByMenuFatherId(x);
                if(menu!=null){
                    if(menu.getState()==1){
                        menuList.add(menu);
                    }
                }
            }
            PageInfo<MenuFather> pageInfo=new PageInfo<>(menuList);
            return ResponseResult.success("用户菜单收藏遍历如下", pageInfo);
        }
        return ResponseResult.error("没有其他类型,只能填写0或1");
    }
}
